<?php

include ('../session.php');
include ('../db.php');
include ('config.php');

$output = '';

$output .= "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
  <head>
    <title>Panic Points - View Member</title>
    <link href=\"../general.css\" rel=\"stylesheet\" type=\"text/css\">
  </head>
  <body>";

include ('navigation.php');

$c_memberid = intval ($memberid);

/*
$query = "select member_id, member_name, race_name, class_name, member_level, sum(earned) as earned, sum(spent) as spent
          from (select members.member_id, member_name, race_name, class_name, member_level, members.member_name, (drops.item_value / attendeecount) as earned, (ifnull(drops2.item_value, 0) + ifnull(drops3.item_value, 0)) as spent
                from " . DKP_MEMBERS . " as members
                inner join " . DKP_RACES . " as races
                on races.race_id = members.race_id
                inner join " . DKP_CLASSES . " as classes
                on classes.class_id = members.class_id
                left outer join " . DKP_ATTENDEES . " as attendees
                on attendees.member_id = members.member_id
                left outer join " . DKP_DROPS . " as drops
                on drops.raid_id = attendees.raid_id
                left outer join " . DKP_DROPS . " as drops2
                on drops2.member_id = members.member_id
                and drops2.drop_id = drops.drop_id
                left outer join " . DKP_DROPS . " as drops3
                on drops3.member_id = members.member_id
                and drops.drop_id is null
                and drops2.drop_id is null
                left outer join (select count(member_id) as attendeecount, raid_id
                                 from " . DKP_ATTENDEES . "
                                 group by raid_id) as attendeecounts
                on attendeecounts.raid_id = attendees.raid_id) as temp
                where member_id = $c_memberid
                group by member_id, member_name
                order by member_name asc";
*/

$query = "select member_id, member_name, race_name, class_name, member_level, sum(earned) as earned, sum(spent) as spent
          from (select members.member_id, member_name, race_name, class_name, member_level, members.member_name, (drops.item_value / attendeecount) as earned, (ifnull(drops2.item_value, 0) + ifnull(drops3.item_value, 0)) as spent
                from " . DKP_MEMBERS . " as members
                inner join " . DKP_RACES . " as races
                on races.race_id = members.race_id
                inner join " . DKP_CLASSES . " as classes
                on classes.class_id = members.class_id
                left outer join " . DKP_ATTENDEES . " as attendees
                on attendees.member_id = members.member_id
                left outer join " . DKP_DROPS . " as drops
                on drops.raid_id = attendees.raid_id
                left outer join " . DKP_DROPS . " as drops2
                on drops2.member_id = members.member_id
                and drops2.drop_id = drops.drop_id
                left outer join " . DKP_DROPS . " as drops3
                on drops3.member_id = members.member_id
                and attendees.member_id is null
                left outer join (select count(member_id) as attendeecount, raid_id
                                 from " . DKP_ATTENDEES . "
                                 group by raid_id) as attendeecounts
                on attendeecounts.raid_id = attendees.raid_id) as temp
                where member_id = $c_memberid
                group by member_id, member_name
                order by member_name asc";
$result = mysql_query ($query);

$row = mysql_fetch_array ($result);

$name = $row['member_name'];
$level = $row['member_level'];
$race = $row['race_name'];
$class = $row['class_name'];
$earned = number_format ($row['earned'], 2);
$spent = number_format ($row['spent'], 2);

$current = number_format ($earned - $spent, 2);

$output .= "
    <span class=\"listtitle\">member data</span>
    <table cellspacing=\"0\" cellpadding=\"0\" style=\"padding: 2px; margin: 0px; border: 1px solid #cccccc\">
      <tr>
        <td class=\"listheader\">character</td>
        <td style=\"text-align: right; padding-left: 10px\">$name</td>
      </tr>
      <tr>
        <td class=\"listheader\">data</td>
        <td style=\"text-align: right; padding-left: 10px\">$level $race $class</td>
      </tr>
      <tr>
        <td class=\"listheader\">earned</td>
        <td style=\"text-align: right; padding-left: 10px\">$earned</td>
        <td></td>
      </tr>
      <tr>
        <td class=\"listheader\">spent</td>
        <td style=\"text-align: right; padding-left: 10px\">$spent</td>
        <td></td>
      </tr>
      <tr>
        <td class=\"listheader\">current</td>
        <td style=\"text-align: right; padding-left: 10px; font-weight: bold\">$current</td>
        <td></td>
      </tr>
    </table>";

$query = "select attendees.raid_id, location_name, event_name, raid_date, sum(item_value / attendeecount) as earned
          from " . DKP_ATTENDEES . " as attendees
          inner join " . DKP_DROPS . " as drops
          on drops.raid_id = attendees.raid_id
          inner join (select count(member_id) as attendeecount, raid_id
                      from " . DKP_ATTENDEES . "
                      group by raid_id) as attendeecounts
          on attendeecounts.raid_id = attendees.raid_id
          inner join " . DKP_RAIDS . " as raids
          on raids.raid_id = attendees.raid_id
          inner join " . DKP_LOCATIONS . " as locations
          on locations.location_id = raids.location_id
          inner join " . DKP_EVENTS . " as events
          on events.event_id= raids.event_id
          where attendees.member_id = $c_memberid
          group by attendees.raid_id
          order by raid_date desc";
$result = mysql_query ($query);

$output .= "
    <span class=\"listtitle\">raids</span>
    <table class=\"list\" cellspacing=\"0\" cellpadding=\"0\">
      <tr class=\"listheader\">
        <td>date</td>
        <td>raid</td>
        <td style=\"text-align: right\">earned</td>
      </tr>";

if (mysql_num_rows ($result) > 0)
{
  $i = 0;
  while ($row = mysql_fetch_array ($result))
  {
    $i++;

    $output .= "
      <tr class=\"" . ($i & 1 ? "listodd" : "listeven") . "\">
        <td>" . $row['raid_date'] . "</td>
        <td><a href=\"viewraid.php?raidid=" . $row['raid_id'] . "\">" . $row['location_name'] . " - " . $row['event_name'] . "</a></td>
        <td style=\"text-align: right\">" . number_format ($row['earned'], 2) . "</td>
      </tr>";
  }
}
else
{
  $output .= "
      <tr class=\"listempty\">
        <td colspan=\"3\">no raids</td>
      </tr>";
}
      
$output .= "
    </table>";


$query = "select raids.raid_id, raid_date, raids.event_id, event_name, drops.item_id, item_name, location_name, drops.item_value
          from " . DKP_DROPS . " as drops
          inner join " . DKP_RAIDS . " as raids
          on raids.raid_id = drops.raid_id
          inner join " . DKP_ITEMS . " as items
          on items.item_id = drops.item_id
          inner join " . DKP_LOCATIONS . " as locations
          on locations.location_id = raids.location_id
          inner join " . DKP_EVENTS . " as events
          on events.event_id = raids.event_id
          where drops.member_id = $c_memberid
          order by raid_date desc";
$result = mysql_query ($query);

echo mysql_error ();

$output .= "
    <span class=\"listtitle\">drops</span>
    <table class=\"list\" cellspacing=\"0\" cellpadding=\"0\">
      <tr class=\"listheader\">
        <td>date</td>
        <td>item</td>
        <td>raid</td>
        <td>value</td>
      </tr>";

if (mysql_num_rows ($result) > 0)
{
  $i = 0;
  while ($row = mysql_fetch_array ($result))
  {
    $i++;

    $output .= "
      <tr class=\"" . ($i & 1 ? "listodd" : "listeven") . "\">
        <td>" . $row['raid_date'] . "</td>
        <td><a href=\"viewitem.php?itemid=" . $row['item_id'] . "\">" . $row['item_name'] . "</a></td>
        <td><a href=\"viewraid.php?raidid=" . $row['raid_id'] . "\">" . $row['location_name'] . " - " . $row['event_name'] . "</a></td>
        <td>" . $row['item_value'] . "</td>
      </tr>";
  }
}
else
{
  $output .= "
      <tr class=\"listempty\">
        <td colspan=\"4\">no drops</td>
      </tr>";
}


$output .= "
    </table>";
      

$output .= "
  </body>
</html>";

echo $output;
